#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct ListNode {
	int val;
	struct ListNode *next;
 };

struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* cur = head;
    struct ListNode* precur = NULL;
    while (cur) {
        if (head->val == val) {
            head = cur->next;
            free(cur);
            cur = head;
        }
        else {
            if (cur->val == val) {
                precur->next = cur->next;
                free(cur);
                cur = precur;
            }
            else {
                precur = cur;
                cur = cur->next;
            }
        }
    }
    return head;
}
void SListprint(struct ListNode* phead) {
    struct ListNode* cur = phead;
    while (cur) {
        printf("%d ", cur->val);
        cur = cur->next;
    }
    printf("\n");
}
int main() {
    struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
    n1->val = 1;
    n2->val = 2;
    n3->val = 3;
    n4->val = 4;
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = NULL;
    SListprint(n1);
    struct ListNode* newphead = removeElements(n1, 1);
    SListprint(newphead);
    return 0;
}